package cn.gcjxsc.controller.api;

import cn.gcjxsc.consts.ErrorConst;
import cn.gcjxsc.data.query.salesman.StoreSettlementQuery;
import cn.gcjxsc.data.vo.salesman.AccountVo;
import cn.gcjxsc.data.vo.salesman.SalesmanVo;
import cn.gcjxsc.entity.salesman.Salesman;
import cn.gcjxsc.entity.salesman.SalesmanAccount;
import cn.gcjxsc.service.integral.ApplyInfoService;
import cn.gcjxsc.service.integral.SalesmanAccountService;
import cn.gcjxsc.service.integral.SalesmanService;
import com.zgxl.common.util.AjaxResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author pumbaa
 * @since 1.0
 */
@RestController("apiSalesmanController")
@RequestMapping("/api")
public class SalesmanController{
    @Autowired
    private ApplyInfoService applyInfoService;
    @Autowired
    private SalesmanService salesmanService;
    @Autowired
    private SalesmanAccountService accountService;

    /**
     * 查询会员所有正在申请的店铺id集
     *
     * @param memberId 会员id
     * @return
     */
    @RequestMapping("/query/memberApplyStoreIds")
    public List<Integer> queryMemberApplyStoreIds(@RequestParam("memberId") Integer memberId) {
        return applyInfoService.queryMemberApplyStoreIds(memberId);
    }

    @RequestMapping("/salesman/find")
    public SalesmanVo findById(@RequestParam("id") Integer id) {
        Salesman salesman = salesmanService.findById(id);
        if (null != salesman) {
            return salesmanService.convert(salesman);
        }
        return null;
    }

    /**
     * 查询会员所有的业务员关系对应的店铺id集
     *
     * @param memberId 会员id
     * @return
     */
    @RequestMapping("/query/memberSalesmanStoreIds")
    public List<Integer> queryMemberSalesmanStoreIds(@RequestParam("memberId") Integer memberId) {
        List<Salesman> salesmanList = salesmanService.findListByMember(memberId);
        List<Integer> storeIds = new ArrayList<>();
        for (Salesman salesman : salesmanList) {
            storeIds.add(salesman.getStoreId());
        }
        return storeIds;
    }

    /**
     * 查询会员是否具有业务员角色.<br>
     * 只要申请过,就算具体该角色.
     *
     * @param memberId 会员id
     * @return
     */
    @RequestMapping("/hasSalesmanRole")
    public boolean hasSalesmanRole(@RequestParam("memberId") Integer memberId) {
        return salesmanService.queryStoresForMember(memberId) > 0;
//        long count = applyInfoService.queryMemberApplyInfoCount(memberId);
//        return count != 0;
    }


    /**
     * 查询业务员id
     *
     * @param memberId 会员id
     * @param storeId  店铺id
     * @return
     */
    @RequestMapping("/salesman/queryId")
    public Integer querySalesmanIdByMemberIdAndStoreId(@RequestParam("memberId") Integer memberId,
                                                       @RequestParam("storeId") Integer storeId) {
        Salesman salesman = salesmanService.findByStoreId(storeId, memberId);
        return salesman == null ? 0 : salesman.getId();
    }

    /**
     * 查询业务员余额
     *
     * @param salesmanId 业务员id
     * @return
     */
    @RequestMapping("/salesman/query/settlementAmount")
    public BigDecimal querySalesmanStoreSettlementAmount(@RequestParam("salesmanId") Integer salesmanId) {
        Salesman salesman = salesmanService.findById(salesmanId);
        if (null == salesman) {
            return BigDecimal.ZERO;
        }
        SalesmanAccount account = accountService.findBySalesman(salesman);
        return null == account ? BigDecimal.ZERO : account.getBalance();
    }

    /**
     * 查询指定会员对应的业务员账户信息
     *
     * @param query 查询条件
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping("/salesman/query/memberSettlementAmount")
    public Map<Integer, AccountVo> queryMemberSettlementAmount(@RequestBody StoreSettlementQuery query) {
        Integer memberId = query.getMemberId();
        List<Integer> stores = query.getStores();
        List<Salesman> salesmanList = salesmanService.findListByMember(memberId);
        List<SalesmanAccount> list = accountService.findByMemberId(memberId);

        Map<Integer, AccountVo> map = new HashMap<>();

        for (Salesman salesman : salesmanList) {
            if (stores.contains(salesman.getStoreId())) {
                for (SalesmanAccount account : list) {
                    if (Objects.equals(account.getSalesmanId(), salesman.getId())) {
                        map.put(salesman.getStoreId(), accountService.convert(account));
                    }
                }
            }
        }
        return map;
    }

    @RequestMapping("/salesman/edit")
    public AjaxResponse editSalesman(@RequestParam("id") Integer id,
                                                 @RequestParam("name") String name,
                                                 @RequestParam("mobile") String mobile) {
        Salesman salesman = salesmanService.findById(id);
        if (null == salesman) {
            return AjaxResponse.ajaxFailure(ErrorConst.RECORD_NOT_EXIST);
        }
        salesman.setName(name);
        salesman.setMobile(mobile);

        try {
            salesmanService.saveOrUpdate(salesman);
            return AjaxResponse.ajaxSuccess();
        } catch (Exception e) {
            return AjaxResponse.ajaxFailure("操作失败[ED01]");
        }
    }
}
